//
//  PersonInfoViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "PersonInfoViewController.h"
#import "PersonInfoCell.h"

@interface PersonInfoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) NSArray *titles;

@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)initUI{
    self.title = LS(@"我的资料");
    NSData * head = [APP.user.ID isEqualToString:@"去登录"] ? [NSData data] : APP.user.headImg;
    self.titles = @[@{@"头像":head},@{@"昵称":APP.user.username},@{@"性别":APP.user.gender},@{@"年龄":APP.user.age}];
    
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[PersonInfoCell nib] forCellReuseIdentifier:[PersonInfoCell ID]];
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c:10];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeBottom c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (void)actionUpdataUser:(NSNotification *)notify{
    [self initUI];
    [self.tableView reloadData];
}
- (void)addNotify{
    [super addNotify];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionUpdataUser:) name:notifyUserUpdate object:nil];
}

- (void)updateUser:(User *)user{
    [UserTool update:user complete:^(NSDictionary * _Nonnull dict) {
//        [Toast startHud:LS(dict[@"msg"]) stopTime:1 handle:nil];
        APP.user = user;
    }];
}
#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    APP.user.headImg = UIImageJPEGRepresentation(img, 0.5);
    [self updateUser:APP.user];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:[PersonInfoCell ID]];
    NSString *title = [[self.titles[indexPath.row] allKeys] firstObject];
    cell.title.text = LS(title);
    cell.titleTop.constant = indexPath.row == 0 ? 26 : 12;
    cell.titleBottom.constant = indexPath.row == 0 ? 26 : 12;
    cell.content.hidden = indexPath.row == 0;
    cell.arrow.hidden = indexPath.row == 0;
    cell.img.hidden = indexPath.row != 0;
    if (indexPath.row == 0 && [APP.user.headImg length] > 0) {
        cell.img.image = [UIImage imageWithData:APP.user.headImg];
    }else if (indexPath.row > 0){
        NSString *content = [[self.titles[indexPath.row] allValues] firstObject];
        cell.content.text = LS(content);
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(self)
    NSString *title = [[self.titles[indexPath.row] allKeys] firstObject];
    if ([title isEqualToString:@"头像"]) {
        SheetViewController *vc = [SheetViewController new];
        vc.array = @[@"拍照",@"从图库中选择"];
        vc.block = ^(int index, NSString * _Nonnull text) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [Toast startHud];
            });
            UIImagePickerController *vc = [UIImagePickerController new];
            vc.delegate = self;
            vc.allowsEditing = YES;
            vc.sourceType = index == 0 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [weakself presentViewController:vc animated:YES completion:^{
                [Toast stopHud];
            }];
        };
        [self presentViewController:vc animated:NO completion:nil];
    }else if ([title isEqualToString:@"昵称"]){
        [AlertPopViewController pop:@"修改昵称" content:@"" type:AlertPopViewControllerTypeInput block:^(int tag, NSString * _Nonnull text) {
            APP.user.username = text;
            [weakself updateUser:APP.user];
        }];
    }else if ([title isEqualToString:@"性别"]){
        SheetViewController *vc = [SheetViewController new];
        vc.array = @[@"男",@"女"];
        vc.block = ^(int index, NSString * _Nonnull text) {
            APP.user.gender = text;
            [weakself updateUser:APP.user];
        };
        [self presentViewController:vc animated:NO completion:nil];
        
    }else if ([title isEqualToString:@"年龄"]){
        DateViewController *vc = [DateViewController new];
        vc.block = ^(int index, NSString * _Nonnull text) {
            APP.user.age = text;
            [weakself updateUser:APP.user];
        };
        [self presentViewController:vc animated:NO completion:nil];
    }
}

@end
